import { Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { DValidateRules, FormLayout } from 'ng-devui';
import { ProcessService } from 'src/app/@core/services/process/process.service';

@Component({
  selector: 'app-edit-technology-route',
  templateUrl: './edit-technology-route.component.html',
  styleUrls: ['./edit-technology-route.component.scss']
})
export class EditTechnologyRouteComponent {
  tabActiveId: string | number = '基本信息';
  technologyRoute={
    technologyRouteNum: "",
    technologyRouteName: "",
    note: "",
    processDetails: []
  }

  technologyRouteRules:{[key:string]:DValidateRules}={
    technologyRouteNum: {
      messageShowType:'text',
      validators: [
        { required: true,message: '工艺路线编码不能为空' },
        { minlength: 2, message: '*工艺路线编码长度至少为2' },
        { maxlength: 40, message: '*工艺路线编码长度至多为40' },
        { pattern: /^[^\s]*$/, message: '*工艺路线编码不能包含空格' }
      ]
    },
    technologyRouteName: {
      messageShowType:'text',
      validators: [
        { required: true,message: '工艺路线名称不能为空' },
        { minlength: 2, message: '*工艺路线名称长度至少为2' },
        { maxlength: 40, message: '*工艺路线名称长度至多为40' },
        { pattern: /^[^\s]*$/, message: '*工艺路线名称不能包含空格' }
      ]
    },
  }
  layoutDirection: FormLayout = FormLayout.Horizontal;
  msgs: { severity: any; summary: any; content: any; }[];
  nodes =[];
  display = false;
  options =[];
  processOptions:[]
  selectNode = this.nodes[0];

  technologyRouteId: any;
  constructor(private ProcessService:ProcessService,private Router:Router,private ActivatedRoute:ActivatedRoute){ }
  ngOnInit(): void {

    this.ActivatedRoute.params.subscribe(params => {
      this.technologyRouteId = params.technologyRouteId; // 打印查询参数
      this.ProcessService.GetTechnologyRouteById(params.technologyRouteId).subscribe((res:any)=>{

        this.technologyRoute = res
        this.nodes = res.processDetails

      })
     this.ProcessService.GetPagedProcesss('',1,9999).subscribe((data:any)=>{
      this.processOptions=data.items;

     })
    })
  }
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  submit(){
    let nodes = this.nodes.map(({ isClicked, ...rest }) => rest).filter(node => node.processId !== null);
    nodes = nodes.map((node, index) => {
      return {
        ...node,
        processNumber: ( index + 1)*10
      };
    });
    if(nodes){
      this.technologyRoute.processDetails = nodes
    }
    if(this.technologyRoute.technologyRouteNum&&this.technologyRoute.technologyRouteName){

      this.ProcessService.UpdateTechnologyRoute(this.technologyRouteId,this.technologyRoute).subscribe((res:any)=>{
        this.showToast('success','成功','更新成功')
        setTimeout(() => {
          this.Router.navigate(['/pages/process/technology-route'])
        },1500)
      },(error) => {
        if(error.status==404){
          this.showToast('error', '失败', '服务器连接失败');
        }else{
          this.showToast('error', '失败', error.error.error.message);
        }
      }
      )
    }else{
      this.showToast('warn','错误','请填写必填项')
    }
  }
  cancle(){
    this.Router.navigate(['/pages/process/technology-route'])
  }
  addNode(node?: any) {
    const newNode = { processNumber: 1, isFirstInspection: false, processId: null, isInspection: false, isOutsourcing:false, processName:'未设置工序',processNum:0 };

    if (!node) {
      this.nodes.unshift(newNode);
    } else {
      const index = this.nodes.indexOf(node);
      this.nodes.splice(index + 1, 0, newNode);
    }
    this.nodes.forEach((item, index) => {
      item.processNumber =( index + 1)*10;
    });

  }
  deleteNode(node?:any,){
    this.nodes = this.nodes.filter(item=>item.processNumber!==node.processNumber)
    this.nodes.forEach((item, index) => {
      item.processNumber = ( index + 1)*10;
    });
    this.display = false;
  }
  editNode(node?:any,i?:any){
    this.display = true;
    this.selectNode = node;
    this.nodes.forEach(node => {
      node.isClicked = false;
    });
    this.selectNode.isClicked = true;
  }
  saveNode(){
  }
  selectGroup($event:any){

  }
  selectProcess($event: any){
    this.selectNode.processId = $event.processId
    this.selectNode.processName = $event.processName
    this.selectNode.processNum = $event.processNum
  }

}
